/**
 * ClassName 部门管理控制器
 */
Ext.define("app.controller.DeptController",{
	
	extend:'Ext.app.Controller',
	init: function(){

		this.control({
			//修改部门信息 弹窗
			'deptlist':{
				itemdblclick:'updateDept',
			},
			//添加部门 弹窗
			'deptlist button[action=add]':{
				click:'addDept',
			},
			//添加部门 提交
			'deptadd button[action=add]':{
				click:'deptAddSubmit',
			},
			//删除部门
			'deptlist button[action=delete]':{
				click:'deleteDept',
			},
			//指定员工部门
			'deptstaff':{
				itemdblclick:'dept_staffedit',
			},
			//添加部门 提交
			'deptupdate button[action=update]':{
				click:'deptUpdateSubmit',
			},
			//查看部门信息 
			'deptlist button[action=check]':{
				click:'deptCheck',
			},
			//设置员工部门  提交
			'deptstaffedit button[action=save]':{
				click:'deptSet',
			},
		})
	},
	//给员工设置部门
	deptSet:function(btn,e,o){
		var win = btn.up("window");
		var form = win.down('form');				
		var record = form.getRecord();
		
		var data = form.getValues();
		data.id=record.getData().id;
		console.log(record.ownerCt);
		var obj = {
				form : form,
				record :  Ext.getCmp('dept-staff').getComponent('deptlist'),
				record2 :  Ext.getCmp('dept-staff').getComponent('deptstaff'),
				win : win,
				data : data,
				url : ROOT+'/index.php/Home/Dept/setDept',
				operate : 'window',
		};
		var saveAjax = new myAjax(obj)	
		saveAjax.request();				
	},
	//双击编辑信息 弹窗
	dept_staffedit:function(grid,record){
		var win = Ext.widget('deptstaffedit');
		win.down("form").loadRecord(record);
		//console.log(record);
		win.show();
	},

			
		
	//双击编辑部门信息 弹窗
	updateDept:function(grid,record){
		var win = Ext.widget('deptupdate');
		win.down("form").loadRecord(record);
		win.show();
	},
	
	//保存编辑的信息 提交
	deptUpdateSubmit:function(btn,e,o){
		var win = btn.up("window");
		var form = win.down('form');
		var record = form.getRecord();				
		var	id = record.get('id');
		var operate = 'update';				
		var data = form.getValues();
		data.id=id;	
		var obj = {
			    form : form,
			    record : record,
				win : win,
				data : data,
				url : ROOT+'/index.php/Home/Dept/update',
				operate : operate,
		};		
		var saveAjax = new myAjax(obj)		
		saveAjax.request();				
	},
	
	//删除员工信息
	deleteDept:function(e){		
		var grid=e.ownerCt.ownerCt;
		var rows=grid.getSelectionModel().getSelection();
		if(rows.length==0){
			Ext.Msg.alert('警告','请至少选中一行');			
		}else{
			//1.获得id
			var ids=[];
			Ext.Array.each(rows,function(record){
				ids.push(record.get('id'));
			})
			var data = {data:ids.join(',')};
			//2.ajax后台操作
		var	obj = {
				rows : rows,
				st : grid.getStore(),
				data : data,
				url : ROOT+'/index.php/Home/Dept/delete',
				operate : 'del',
		};	

			var delAjax = new myAjax(obj);
			delAjax.request();

		}
		
	},
	
	
	//添加部门 弹窗
	addDept:function(e){
		var win = Ext.widget('deptadd');
		win.show();		
	},
	
	//添加部门 提交
	deptAddSubmit : function(btn, e, eOpts){
		var obj = {
				win : btn.up("window"),
				data : btn.up("window").down('form').getValues(),
				url : ROOT+'/index.php/Home/Dept/add',
				operate : 'window',
				record : Ext.getCmp('dept-staff').getComponent('deptlist'),
		};	
		var addAjax =new myAjax(obj);	
		addAjax.request();  
	},
	
	//点击左边导航 右边弹窗对应的面板
	deptCheck:function(btn, e){
		var panel = Ext.getCmp('center-grid');
		var grid=btn.ownerCt.ownerCt;
		var rows=grid.getSelectionModel().getSelection();
		if(rows.length != 1){
			Ext.Msg.alert('警告','只能选择一行');
			return false;
		}
		var data = {};
		Ext.Array.each(rows,function(record){
			data = record.getData();			
			//		
			var card ={
					title: data.name,				
					closable: true,
					id:'dept'+data.id,
					xtype:'deptstaff',			
				};						
		})
		data.start = 0;	
		var store = Ext.getCmp('dept-staff').getComponent('deptstaff').getStore()
		store.load({params:data});//加载store时传参！！！！	
		store.on("beforeload",function(){
		    Ext.apply(store.proxy.extraParams, {id:data.id});
		});
	},	
	
	views:[
	       'dept.Edit',
	       'dept.Dept-Staff',
	       'dept.List',
	       'dept.Add',
	       'dept.Update',
	       'dept.Form',
	       'dept.Staff',
	       ],
	stores:[
	        'dept.ListStore',
	        'dept.SelectStore',
	        'dept.StaffStore'
	       ],
	//models:['StaffModel'],
})